# Copyright (c) 2022, hzmes and contributors
# For license information, please see license.txt

import frappe
from frappe.model.document import Document
from frappe.utils import cint, cstr, flt


class SRMDeliveryChecking(Document):
	def after_insert(self):
		print('\n\n\n\n\n\n\nAdd Receive Checking\n\n\n\n\n\n\n')
		row_delivery = frappe.get_doc('SRM Purchase Delivery Sub', self.delivery_sub_name)

		if self.srm_receive_sub:
			row_receive = frappe.get_doc('SRM Receive Sub', self.srm_receive_sub)
			row_delivery.check_in_qty += flt(self.check_in_qty)
			row_delivery.remain_qty = flt(row_delivery.remain_qty) - flt(self.check_in_qty)
			row_receive.check_qty += flt(self.check_in_qty)
			row_receive.save(ignore_permissions=True)
		elif self.srm_return_name:
			row_return = frappe.get_doc('SRM Instock Return Sub', self.srm_return_name)
			row_delivery.check_out_qty += flt(self.check_out_qty)
			row_return.check_qty += flt(self.check_out_qty)
			row_return.save(ignore_permissions=True)
			# row_delivery.remain_qty = flt(row_delivery.remain_qty) - flt(self.check_out_qty)

		row_delivery.save(ignore_permissions=True)


	def after_delete(self):
		print('\n\n\n\n\n\n\nDel Receive Checking\n\n\n\n\n\n')
		row_delivery = frappe.get_doc('SRM Purchase Delivery Sub', self.delivery_sub_name)
		if self.srm_receive_sub:
			row_receive = frappe.get_doc('SRM Receive Sub', self.srm_receive_sub)
			row_delivery.check_in_qty = flt(row_delivery.check_in_qty) - flt(self.check_in_qty)
			row_delivery.remain_qty += flt(self.check_in_qty)
			row_receive.check_qty = flt(row_receive.check_qty) - flt(self.check_in_qty)
			row_receive.save(ignore_permissions=True)
		elif self.srm_return_name:
			row_return = frappe.get_doc('SRM Instock Return Sub', self.srm_return_name)
			row_delivery.check_out_qty = flt(row_delivery.check_out_qty) - flt(self.check_out_qty)
			row_return.check_qty = flt(row_return.check_qty) - flt(self.check_out_qty)
			row_return.save(ignore_permissions=True)
			# row_delivery.remain_qty = row_delivery.remain_qty - flt(self.check_out_qty)
		row_delivery.save(ignore_permissions=True)


@frappe.whitelist()
# srm.srm.doctype.srm_delivery_checking.srm_delivery_checking.relation_create
def relation_create(supplier, po_entryid, delivery_sub_name, material_number, srm_receive_sub=None, srm_return_name=None,   reveive_qty=0, return_qty=0, po_billno=None, uom=None):
	print(f'\n\n\n\n\n\n\n{reveive_qty}\n\n\n\n\n\n')
	print(f'\n\n\n\n\n\n\n{return_qty}\n\n\n\n\n\n')
	row_delivery_sub = frappe.get_doc('SRM Purchase Delivery Sub', delivery_sub_name)
	if srm_receive_sub:
		row_receive_sub = frappe.get_doc('SRM Receive Sub', srm_receive_sub)
		remain_qty = row_delivery_sub.remain_qty
		doc = frappe.get_doc({
			'doctype': 'SRM Delivery Checking',
			'supplier': supplier,
			'po_billno': po_billno,
			'srm_receive': row_receive_sub.parent,
			'srm_receive_sub': srm_receive_sub,
			'srm_return': None,
			'srm_return_name': None,
			'delivery_billno': row_delivery_sub.parent,
			'po_entryid': po_entryid,
			'delivery_sub_name': delivery_sub_name,
			'material_number': material_number,
			'uom': uom,
			'check_in_qty': flt(reveive_qty) if flt(reveive_qty) <= flt(remain_qty) else flt(remain_qty),
			'check_out_qty': 0,
		})
	if srm_return_name:
		row_return = frappe.get_doc('SRM Instock Return Sub', srm_return_name)
		doc = frappe.get_doc({
			'doctype': 'SRM Delivery Checking',
			'supplier': supplier,
			'po_billno': po_billno,
			'srm_receive': None,
			'srm_receive_sub': None,
			'srm_return': row_return.parent,
			'srm_return_name': srm_return_name,
			'delivery_billno': row_delivery_sub.parent,
			'po_entryid': po_entryid,
			'delivery_sub_name': delivery_sub_name,
			'material_number': material_number,
			'uom': uom,
			'check_in_qty': 0,
			'check_out_qty': flt(return_qty),
		})
	
	doc.insert(ignore_permissions=True)
	return doc

